home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Applications / DropBin 1.5 / BinProgress.p < prev    next >
Text File  |  1997-04-16  |  9KB  |  364 lines

  1. Unit BinProgress;
  2. {$NR+}
  3.  
  4. Interface
  5.  
  6. Uses
  7.     Toolbox, DropBinUtils, Palettes;
  8.  
  9. Const
  10.     kDialogOKHit = 1;
  11.     kDialogCancelHit = 2;
  12.     
  13. Var
  14.     infoRect,
  15.     infoRect2,
  16.     statusRect,
  17.     countRect,
  18.     progressRect,
  19.     filledRect,
  20.     emptyRect:        rect;
  21.     curAmount:        longint;
  22.     totalAmount:    longint;
  23.     dark,light:        RGBColor;
  24.     backGround:        RGBColor;
  25.         
  26. Procedure MakeDefaultButton(theButton: ControlHandle);
  27. Procedure DisplayStatus;
  28. Procedure ResetWindow(wp: WindowPtr);
  29. Procedure SetupProgress;
  30. Procedure DoProgressEvent(var theEvent: EventRecord; var keyPressed: integer);
  31. Procedure StartProgress(len: longint);
  32. Procedure EndProgress;
  33. Procedure MakeColor(var col: RGBColor; red, green, blue: integer);
  34. Function UpdateProgress(amt: longint): integer;
  35.  
  36. Implementation
  37.  
  38. Procedure MakeDefaultButton(theButton: ControlHandle);
  39.  
  40. Const
  41.     kButtonFrameInset = -4;
  42.     kButtonFrameSize = 3;
  43.     kCntrlActivate = 0;
  44.  
  45. Var
  46.     itemRect:        Rect;                
  47.     curPen:            PenState;
  48.     buttonOval:        Integer;
  49.     fgSaveColor:    RGBColor;
  50.     bgColor:        RGBColor;
  51.     newfgColor:        RGBColor;
  52.     newGray:        Boolean;
  53.     oldPort:        WindowPtr;
  54.     isColor:        Boolean;
  55.  
  56.     begin
  57.     { draw a bold border around default button }
  58.     GetPort(oldPort);
  59.     SetPort(theButton^^.contrlOwner);
  60.     GetPenState(curPen);
  61.     PenNormal;
  62.     itemRect := theButton^^.contrlRect;
  63.     InsetRect(itemRect, kButtonFrameInset, kButtonFrameInset);
  64.     FrameRoundRect(itemRect, 16, 16);
  65.     buttonOval := (itemRect.bottom - itemRect.top) DIV 2 + 2;
  66.     if true then
  67.         isColor := TRUE
  68.     else
  69.         isColor := FALSE;
  70.     if theButton^^.contrlHilite <> kCntrlActivate then
  71.         begin            {control is dimmed, so draw default button outline}
  72.         newGray := FALSE;
  73.         if isColor then
  74.             begin
  75.             GetBackColor(bgColor);
  76.             GetForeColor(fgSaveColor);
  77.             newfgColor := fgSaveColor;
  78.             {use the gray defined by the current device}
  79.             newGray := GetGray(GetGDevice, bgColor, newfgColor);
  80.             end;
  81.         if newGray then
  82.             RGBForeColor(newfgColor)
  83.         else
  84.             PenPat(qd.gray);
  85.         PenSize(kButtonFrameSize, kButtonFrameSize);
  86.         FrameRoundRect(itemRect, buttonOval, buttonOval);
  87.         if isColor then
  88.             RGBForeColor(fgSaveColor);
  89.         end
  90.     else        {control is active, so draw default button outline in black}
  91.         begin
  92.         PenPat(qd.black);
  93.         PenSize(kButtonFrameSize, kButtonFrameSize);
  94.         FrameRoundRect(itemRect, buttonOval, buttonOval);
  95.         end;
  96.     SetPenState(curPen);
  97.     SetPort(oldPort);
  98.     end; { of MakeDefaultButton }
  99.  
  100. Procedure DisplayProgress;
  101.  
  102. Var
  103.     str:    str255;
  104.     r:        rect;
  105.     w, uw:     integer;
  106.     
  107.     begin
  108.     SetPort(dbWindow);
  109.     case gStatType of
  110.     kShowRemaining:    str := DBFormat(MaxValue(0,totalAmount - curAmount));
  111.     kShowProcessed:    str := DBFormat(curAmount);
  112.     kShowTotal:        str := DBFormat(totalAmount);
  113.     {case}            end;
  114.     StringToRect(str, countRect, 9, []);
  115.     FrameRect(progressRect);
  116.     r := progressRect;
  117.     insetRect(r,1,1);
  118.     if totalAmount < 0 then
  119.         EraseRect(r)
  120.     else 
  121.         begin
  122.         w := r.right - r.left;
  123.         if totalAmount <= 0 then
  124.             uw := 0
  125.         else if curAmount >= totalAmount then 
  126.             uw := w
  127.         else
  128.             uw := FracMul(w, FracDiv(curAmount, totalAmount));
  129.         filledRect := r;
  130.         emptyRect := r;
  131.         filledRect.right := r.left + uw;
  132.         emptyRect.left :=  r.left + uw;
  133.         RGBForeColor(dark);
  134.         RGBBackColor(light);
  135.         PaintRect(filledRect);
  136.         RGBForeColor(light);
  137.         RGBBackColor(dark);
  138.         PaintRect(emptyRect);
  139.         ForeColor(blackColor);
  140.         RGBBackColor(backGround);
  141.         end;
  142.     end; { of DisplayProgress }
  143.     
  144. Procedure DisplayStatus;
  145.  
  146. Var
  147.     str:    str255;
  148.     
  149.     begin
  150.     StringToRect('Encoding: ', infoRect, 9, [bold]);
  151.     AppendToRect(gFilename);
  152.     StringToRect('Output file: ', infoRect2, 9, [bold]);
  153.     AppendToRect(gOutputName);
  154.     if gStatType = kShowRemaining then
  155.         str := 'Remaining: '
  156.     else if gStatType = kShowProcessed then
  157.         str := 'Processed: '
  158.     else 
  159.         str := 'Total: ';
  160.     StringToRect(str, statusRect, 9, [bold]);
  161.     countRect := statusRect;
  162.     countRect.left := countRect.left + StringWidth(str);
  163.     DisplayProgress;
  164.     end; { of DisplayStatus }
  165.     
  166. Procedure ResetWindow(wp: WindowPtr);
  167.  
  168.     begin
  169.     if (wp <> dbWindow) or ((not gOApped) and (not gProcessing)) then
  170.         exit(ResetWindow);
  171.     SetPort(wp);
  172.     BeginUpdate(wp);
  173.     if gProcessing then
  174.         begin
  175.         EraseRect(wp^.portRect);
  176.         DisplayStatus;
  177.         DrawControls(wp);
  178.         end
  179.     else
  180.         begin
  181.         EraseRect(wp^.portRect);
  182.         DrawControls(wp);
  183.         MakeDefaultButton(encodeButton);
  184.         StringToRect('Ready to encode a file.', infoRect, 0, []);
  185.         end;
  186.     EndUpdate(wp);
  187.     end;
  188.  
  189. Procedure SetupProgress;
  190.  
  191.     begin
  192.     if dbWindow = NIL then
  193.         begin
  194.         setRect(infoRect, 10, 10, 250, 30);
  195.         setRect(infoRect2, 10, 35, 250, 55);
  196.         setRect(statusRect, 10, 60, 250, 80);
  197.         setRect(progressRect, 10, 85, 250, 97);
  198.         MakeColor(dark, $4000, $4000, $4000);
  199.         MakeColor(light, $CCCC, $CCCC, $FFFF);
  200.         dbWindow := GetNewCWindow(128, NIL, Pointer(-1));
  201.         if gOApped then
  202.             begin
  203.             encodeButton := GetNewControl(128, dbWindow);
  204.             MoveControl(encodeButton, 50, 95); 
  205.             MakeDefaultButton(encodeButton);
  206.             quitButton := GetNewControl(129, dbWindow);
  207.             MoveControl(quitButton, 150, 95); 
  208.             ShowControl(encodeButton);
  209.             ShowControl(quitButton);
  210.             end;
  211.         cancelButton := GetNewControl(130, dbWindow);
  212.         MoveControl(cancelButton, progressRect.right + 10, progressRect.top - 5); 
  213.         if dbWindow <> NIL then
  214.             begin
  215.             ShowWindow(dbWindow);
  216.             SetPort(dbWindow);
  217.             GetBackColor(backGround);
  218.             ResetWindow(dbWindow);
  219.             end;
  220.         end
  221.     else
  222.         begin
  223.         ForeColor(blackColor);
  224.         RGBBackColor(backGround);
  225.         end;
  226.     end;
  227.  
  228. Procedure DoProgressEvent(var theEvent: EventRecord; var keyPressed: integer);
  229.  
  230. Var
  231.     theWindow: WindowPtr;
  232.     aRect: Rect;
  233.     thePart: integer;
  234.     keyCode: integer;
  235.     control: ControlRef;
  236.     finalTicks: longint;
  237.     
  238.     begin
  239.     keyPressed := 0;
  240.     case theEvent.what of
  241.         mouseDown:        begin
  242.                         thePart := FindWindow(theEvent.where, theWindow);
  243.                         case thePart of
  244.                             inDrag:        if theWindow = dbWindow then
  245.                                             begin
  246.                                             aRect := qd.screenBits.bounds;
  247.                                             InsetRect(aRect, -4, -4);
  248.                                             DragWindow(theWindow, theEvent.where, aRect);
  249.                                             end;
  250.                             inContent:    if theWindow = dbWindow then
  251.                                             begin
  252.                                             SetPort(dbWindow);
  253.                                             GlobalToLocal(theEvent.where);
  254.                                             if FindControl(theEvent.where, dbWindow, 
  255.                                                         control) > 0 then
  256.                                                 begin
  257.                                                 if TrackControl(control, 
  258.                                                             theEvent.where, nil) > 0 then
  259.                                                     if control = cancelButton then
  260.                                                         keyPressed := kDialogCancelHit;
  261.                                                 end
  262.                                             else if PtInRect(theEvent.where, statusRect) then
  263.                                                 begin
  264.                                                 gStatType := (gStatType + 1) mod 3;
  265.                                                 InvalRect(dbWindow^.portRect);
  266.                                                 end;
  267.                                             end;
  268.                             inMenuBar:    MenuSelect(theEvent.where);
  269.                         {CASE}            end;
  270.                         end;
  271.         keyDown,
  272.         autokey:        if FrontWindow = dbWindow then
  273.                             begin
  274.                             keyCode := BAnd(theEvent.message, charCodeMask);
  275.                             if (keyCode = kEscapeKey) or ((keyCode = integer('.')) and 
  276.                                (BAnd(theEvent.modifiers,cmdKey) = cmdKey)) then
  277.                                 begin
  278.                                 HiliteControl(cancelButton, kControlButtonPart);
  279.                                 Delay(8, finalTicks);        {invert button for 8 ticks}
  280.                                 HiliteControl(cancelButton, 0);
  281.                                 keyPressed := kDialogCancelHit;
  282.                                 end;
  283.                             end;
  284.         osEvt:            if BAnd(brotl(theEvent.message,8),$FF) = suspendResumeMessage then
  285.                             begin
  286.                             gBackground := BAnd(theEvent.message,resumeFlag) = 0;
  287.                             if gBackground then 
  288.                                 HiliteControl(cancelButton, 255)
  289.                             else 
  290.                                 HiliteControl(cancelButton, 0);
  291.                             end;
  292.         updateEvt:        begin
  293.                         InvalRect(WindowPtr(theEvent.message)^.portRect);
  294.                         ResetWindow(WindowPtr(theEvent.message));
  295.                         end;
  296.     {CASE}                end;
  297.     end; { of DoProgressEvent }
  298.  
  299. Procedure StartProgress(len: longint);
  300.  
  301. Var
  302.     theRect:    rect;
  303.     
  304.     begin
  305.     totalAmount := len;
  306.     curAmount := 0;
  307.     SetPort(dbWindow);
  308.     EraseRect(progressRect);
  309.     ShowControl(cancelButton);
  310.     DisplayStatus;
  311.     theRect := encodeButton^^.contrlRect;
  312.     InsetRect(theRect,-16,-16);
  313.     EraseRect(theRect);
  314.     if gOApped then
  315.         begin
  316.         HideControl(quitButton);
  317.         HideControl(encodeButton);
  318.         end;
  319.     DisableItem(gAppleMenu,0);
  320.     DisableItem(gFileMenu,0);
  321.     end;
  322.     
  323. Procedure EndProgress;
  324.  
  325.     begin
  326.     SetPort(dbWindow);
  327.     EnableItem(gAppleMenu,0);
  328.     EnableItem(gFileMenu,0);
  329.     HideControl(cancelButton);
  330.     if gOApped then
  331.         begin
  332.         ShowControl(quitButton);
  333.         ShowControl(encodeButton);
  334.         end;
  335.     DrawMenuBar;
  336.     end;
  337.  
  338. Procedure MakeColor(var col: RGBColor; red, green, blue: integer);
  339.  
  340.     begin
  341.     col.red := red;
  342.     col.green := green;
  343.     col.blue := blue;
  344.     end;
  345.     
  346. Function UpdateProgress(amt: longint): integer;
  347.  
  348. Var
  349.     theEvent:        EventRecord;
  350.     keyPressed:        integer;
  351.     
  352.     begin
  353.     keyPressed := 0;
  354.     curAmount := curAmount + amt;
  355.     if GetNextEvent(everyEvent, theEvent) then
  356.         DoProgressEvent(theEvent, keyPressed);
  357.     UpdateProgress := keyPressed;
  358.     if keyPressed = kDialogCancelHit then  { if interrupt, then exit }
  359.         exit(UpdateProgress);
  360.     DisplayProgress;
  361.     end;
  362.  
  363. End.
  364.